﻿//-----------------------------------------------------------------------
// <copyright file="BaseDuplicateFinderTester.cs" company="Anthony Steele">
// This source forms part of the DuplicateFinder utility
// Hosted at http://www.codeplex.com/DuplicateFinder
//
// This source is available under the terms of the Microsoft Public License (Ms-PL)
// See http://duplicatefinder.codeplex.com/license
// All other rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DuplicateFinder.TestLibrary
{
    using DuplicateFinderLib;

    /// <summary>
    /// base class for both file and raw duplicate finders
    /// wrapper class to record numbers 
    /// on what a duplicate finder does when it runs
    /// </summary>
    public class BaseDuplicateFinderTester
    {
        #region private data

        // data recorded about duplicates found

        /// <summary>
        /// Number of duplicates found
        /// </summary>
        private int duplicateCount;

        /// <summary>
        /// Length of the last duplicate
        /// </summary>
        private int lastDuplicateLength;

        /// <summary>
        /// Number of copies of the last duplicate
        /// </summary>
        private int lastDuplicateInstanceCount;

        /// <summary>
        /// first line of the last duplicate
        /// </summary>
        private int lastDuplicateFirstLine;

        /// <summary>
        /// length of longest duplicate
        /// </summary>
        private int maxDuplicateLength;

        /// <summary>
        /// largest number of instances found
        /// </summary>
        private int maxDuplicateInstanceCount;

        #endregion

        #region properties

        /// <summary>
        /// Gets how many duplicates were found
        /// </summary>
        public int DuplicateCount
        {
            get { return this.duplicateCount; }
        }

        /// <summary>
        /// Gets the length of the last duplicate
        /// </summary>
        public int LastDuplicateLength
        {
            get { return this.lastDuplicateLength; }
        }

        /// <summary>
        /// Gets  the instance count of the last duplicate
        /// </summary>
        public int LastDuplicateInstanceCount
        {
            get { return this.lastDuplicateInstanceCount; }
        }

        /// <summary>
        /// Gets the first line of the last duplicate
        /// </summary>
        public int LastDuplicateFirstLine
        {
            get { return this.lastDuplicateFirstLine; }
        }

        /// <summary>
        /// Gets the highest instance count
        /// </summary>
        public int MaxDuplicateInstanceCount
        {
            get { return this.maxDuplicateInstanceCount; }
        }

        /// <summary>
        /// Gets the highest length
        /// </summary>
        public int MaxDuplicateLength
        {
            get { return this.maxDuplicateLength; }
        }

        #endregion

        #region event handler

        /// <summary>
        /// get notification of a duplicate that was found
        /// Keep count
        /// </summary>
        /// <param name="sender">the engine that sent the event</param>
        /// <param name="e">event data</param>
        protected void OnDuplicateHandler(object sender, DuplicateEventArgs e)
        {
            this.duplicateCount++;
            this.lastDuplicateLength = e.Length;
            this.lastDuplicateInstanceCount = e.Items.Count;
            this.lastDuplicateFirstLine = e.Items[0].LineNumber;

            if (this.lastDuplicateLength > this.maxDuplicateLength)
            {
                this.maxDuplicateLength = this.lastDuplicateLength;
            }

            if (this.lastDuplicateInstanceCount > this.maxDuplicateInstanceCount)
            {
                this.maxDuplicateInstanceCount = this.lastDuplicateInstanceCount;
            }
        }

        #endregion
    }
}
